package com.hpradip.android.documentaryHub;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class RSSFeedHandler extends DefaultHandler {
	private static final int RSS_TITLE = 1;
	private static final int RSS_DESCRIPTION = 2;
	private static final int RSS_LINK = 3;
	private static final int RSS_CATEGORY = 4;
	private static final int RSS_PUBDATE = 5;

	private int currentstate;
	private DocumentaryFeed _item;
	private int depth;
	private List<DocumentaryFeed> _feed;
	private String previousString;

	public List<DocumentaryFeed> getFeedItems() {
		return _feed;
	}

	@Override
	public void startDocument() throws SAXException {
		_feed = new ArrayList<DocumentaryFeed>();
		_item = new DocumentaryFeed();
	}

	@Override
	public void endDocument() throws SAXException {
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		depth++;
		if (localName.equals("channel")) {
			previousString="";
			currentstate = 0;
			return;
		}
		if (localName.equals("item")) {
			previousString="";
			_item = new DocumentaryFeed();
			return;
		}
		if (localName.equals("title")) {
			previousString="";
			currentstate = RSS_TITLE;
			return;
		}
		if (localName.equals("description")) {
			previousString="";
			currentstate = RSS_DESCRIPTION;
			return;
		}
		if (localName.equals("link")) {
			previousString="";
			currentstate = RSS_LINK;
			return;
		}
		if (localName.equals("category")) {
			previousString="";
			currentstate = RSS_CATEGORY;
			return;
		}
		if (localName.equals("pubDate")) {
			previousString="";
			currentstate = RSS_PUBDATE;
			return;
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		depth--;
		if (localName.equals("channel")) {
			previousString="";
			currentstate = 0;
			return;
		}
		if (localName.equals("title")) {
			previousString="";
			currentstate = 0;
			return;
		}
		if (localName.equals("description")) {
			previousString="";
			currentstate = 0;
			return;
		}
		if (localName.equals("link")) {
			previousString="";
			currentstate = 0;
			return;
		}
		if (localName.equals("category")) {
			previousString="";
			currentstate = 0;
			return;
		}
		if (localName.equals("pubDate")) {
			previousString="";
			currentstate = 0;
			return;
		}
		
		if (localName.equals("item")) {
			_feed.add(_item);
			currentstate =0;
			return;
		}
	} 

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		String theString = new String(ch, start, length);
		Log.i("RSSFeedHandler", "characters[" + theString + "]");

		if(currentstate!=0)
			theString = previousString + theString;
		
		switch (currentstate) {
		case RSS_TITLE:
			_item.setTitle(theString);
			break;
		case RSS_LINK:
			_item.setLink(theString);
			break;
		case RSS_DESCRIPTION:
			_item.setDescription(theString);
			break;
		case RSS_CATEGORY:
			_item.setCategory(theString);
			break;
		case RSS_PUBDATE:
			_item.setDate(theString);
			break;
		default:
			return;
		}
	}

}
